<html>
<head><meta charset="utf-8"><title>volatile, atomics and mmio · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html">volatile, atomics and mmio</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="171724599"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171724599" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171724599">(Jul 25 2019 at 20:27)</a>:</h4>
<p>for embedded sometimes you need a non-tearing non-coalescing store, is there a way to do this in rust currently? volatile can be tearing afaik, and atomics can be coalesced so i’m not sure how to proceed</p>



<a name="171724677"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171724677" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171724677">(Jul 25 2019 at 20:28)</a>:</h4>
<p>specifically for memory-mapped I/O, e.g if you’re programming the LAPIC on x86 you need such a store</p>



<a name="171724861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171724861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171724861">(Jul 25 2019 at 20:30)</a>:</h4>
<p>no there is not -- the intended way is to use volatile, but we need a way to express when those can tear and when not</p>



<a name="171724972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171724972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171724972">(Jul 25 2019 at 20:32)</a>:</h4>
<p>the two proposals I have seen for that are to either expose some kind of intrinsic or trait that lets you test if volatile accesses for type <code>T</code> can tear, or alternatively <span class="user-mention" data-user-id="132920">@gnzlbg</span> proposed to deprecate <code>read/write_volatile</code> in favor of a whole bunch of non-tearing intrinsics of various sizes (similar to how we have atomic intrinsics of various sizes)</p>



<a name="171725024"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171725024" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171725024">(Jul 25 2019 at 20:32)</a>:</h4>
<p>I think this has stalled mostly because nobody pushed for it</p>



<a name="171725102"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171725102" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171725102">(Jul 25 2019 at 20:33)</a>:</h4>
<p>i see. seems like something that a lot of embedded people would actually need in theory, although in practice i guess volatile will be fine (if it’s aligned)</p>



<a name="171725225"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171725225" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171725225">(Jul 25 2019 at 20:34)</a>:</h4>
<p>in practice "small" aligned volatile accesses (unaligned ones are not even possible on stable Rust) will not tear</p>



<a name="171725250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171725250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171725250">(Jul 25 2019 at 20:35)</a>:</h4>
<p>but I dont know what "small" is. I expect <code>usize</code> to be "small".</p>



<a name="171737519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171737519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171737519">(Jul 25 2019 at 23:58)</a>:</h4>
<p>if you do anything at all mmio you need to check your hardware manual to read the mmio section, so it's not a huge deal that you also have to check the instruction set to see what the allowed read and write sizes are. then you'll know which volatile actions are non-tearing</p>



<a name="171768204"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171768204" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171768204">(Jul 26 2019 at 12:13)</a>:</h4>
<p>the problem, afaik, is more that you can tell the compiler to emit a 64-bit write and it just decides to emit two 32-bit writes for whatever reason, even if 64-bit is the native word size</p>



<a name="171768234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171768234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171768234">(Jul 26 2019 at 12:14)</a>:</h4>
<p>although in practice i can’t think of a reason why llvm would do that</p>



<a name="171771713"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171771713" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171771713">(Jul 26 2019 at 13:05)</a>:</h4>
<p><span class="user-mention" data-user-id="228354">@Matt Taylor</span> I suppose that depends on how you tell the compiler</p>



<a name="171771761"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171771761" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171771761">(Jul 26 2019 at 13:05)</a>:</h4>
<p>if you tell it via a <code>*ptr</code> or <code>ptr.write</code> or similar generic methods that will work for all sizes, the compiler might do whatever, from emitting a single instruction, to two instructions, or even calling <code>memcpy</code></p>



<a name="171771832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171771832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171771832">(Jul 26 2019 at 13:06)</a>:</h4>
<p>if you want the compiler to use a particular instruction to perform a write, AFAICT your only option right now is to use inline assembly</p>



<a name="171771841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171771841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171771841">(Jul 26 2019 at 13:06)</a>:</h4>
<p>yeah, exactly</p>



<a name="171771905"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171771905" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171771905">(Jul 26 2019 at 13:07)</a>:</h4>
<p>you can do a bit better with atomics</p>



<a name="171771929"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171771929" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171771929">(Jul 26 2019 at 13:07)</a>:</h4>
<p>but if you want to do mmio you probably need volatile as well</p>



<a name="171771958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171771958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171771958">(Jul 26 2019 at 13:07)</a>:</h4>
<p>otherwise the write might be optimized in subtle ways</p>



<a name="171772021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772021">(Jul 26 2019 at 13:08)</a>:</h4>
<p>i think in c++ you could use something like volatile std::atomic&lt;T&gt; but since the volatile ops are functions, can’t be done in rust</p>



<a name="171772061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772061">(Jul 26 2019 at 13:09)</a>:</h4>
<p>although it might be worth mentioning that the proposal for the new volatile_load / volatile_store in c++ do guarantee non-tearing if it’s available</p>



<a name="171772072"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772072" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772072">(Jul 26 2019 at 13:09)</a>:</h4>
<p>Are there <code>volatile</code>-qualified overloads of the <code>std::atomic</code> methods in C++ ?</p>



<a name="171772107"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772107" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772107">(Jul 26 2019 at 13:09)</a>:</h4>
<p>we could add <code>volatile_load</code>/<code>volatile_store</code> methods to the atomic types in Rust</p>



<a name="171772126"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772126" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772126">(Jul 26 2019 at 13:09)</a>:</h4>
<p>but the "non-tearing if its available" is not enough if you require non-tearing for correctness</p>



<a name="171772184"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772184" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772184">(Jul 26 2019 at 13:10)</a>:</h4>
<p>you’d also need a way to test for it i guess</p>



<a name="171772192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772192">(Jul 26 2019 at 13:10)</a>:</h4>
<p>or get a compiler error</p>



<a name="171772216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772216">(Jul 26 2019 at 13:11)</a>:</h4>
<p>how would you even tell llvm to emit a non-tearing volatile access</p>



<a name="171772233"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772233" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772233">(Jul 26 2019 at 13:11)</a>:</h4>
<p>right now, the <code>ptr.write_volatile</code> methods are non-tearing, if possible</p>



<a name="171772238"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772238" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772238">(Jul 26 2019 at 13:11)</a>:</h4>
<p>because llvm won't tear for fun</p>



<a name="171772243"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772243" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772243">(Jul 26 2019 at 13:11)</a>:</h4>
<p><span class="user-mention" data-user-id="228354">@Matt Taylor</span> you use a volatile+atomic load/store</p>



<a name="171772254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772254">(Jul 26 2019 at 13:12)</a>:</h4>
<p>ok i see</p>



<a name="171772303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772303">(Jul 26 2019 at 13:12)</a>:</h4>
<p>atomic load/stores are non-tearing (they are "atomic"), and you just need to also prevent llvm from optimizing them in certain ways, which you can do by making them volatile</p>



<a name="171772305"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772305" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772305">(Jul 26 2019 at 13:12)</a>:</h4>
<p>i mean the closest reason i can think to why it might tear is if you have something like a mul which can spit out a 64-bit operand in two regs</p>



<a name="171772316"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772316" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772316">(Jul 26 2019 at 13:12)</a>:</h4>
<p>unaligned load/stores often tear</p>



<a name="171772328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772328">(Jul 26 2019 at 13:12)</a>:</h4>
<p>but as ralf said those are curently not allowed anyway</p>



<a name="171772330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772330">(Jul 26 2019 at 13:12)</a>:</h4>
<p>e.g. if the memory goes across a cache line or page boundary</p>



<a name="171772349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772349">(Jul 26 2019 at 13:13)</a>:</h4>
<p>yep I don't think we have any API for those</p>



<a name="171772362"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772362" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772362">(Jul 26 2019 at 13:13)</a>:</h4>
<p>i mean does llvm actually provide any documented guarantees that it will not tear?</p>



<a name="171772371"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772371" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772371">(Jul 26 2019 at 13:13)</a>:</h4>
<p>for atomics? yes</p>



<a name="171772419"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772419" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772419">(Jul 26 2019 at 13:14)</a>:</h4>
<p>for non-atomics</p>



<a name="171772425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772425">(Jul 26 2019 at 13:14)</a>:</h4>
<p>you said ptr.write_volatile will not tear ‘if possible’</p>



<a name="171772426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772426">(Jul 26 2019 at 13:14)</a>:</h4>
<p>no</p>



<a name="171772432"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772432" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772432">(Jul 26 2019 at 13:14)</a>:</h4>
<p>"if possible" is not a guarantee</p>



<a name="171772470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772470">(Jul 26 2019 at 13:15)</a>:</h4>
<p>fair enough</p>



<a name="171772483"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772483" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772483">(Jul 26 2019 at 13:15)</a>:</h4>
<p>the difference is that atomics will fail to compile if the target doesn't support a particular size</p>



<a name="171772496"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772496" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772496">(Jul 26 2019 at 13:15)</a>:</h4>
<p>so LLVM knows that there is an instruction for doing the thing correctly</p>



<a name="171772574"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772574" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772574">(Jul 26 2019 at 13:16)</a>:</h4>
<p>I think we could add <code>volatile_load</code>/<code>volatile_store</code> methods to the atomic types, and that might work for you - if the target supports that atomic type that is</p>



<a name="171772602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171772602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171772602">(Jul 26 2019 at 13:16)</a>:</h4>
<p>i think that would be a pretty good way to proceed</p>



<a name="171774145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171774145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171774145">(Jul 26 2019 at 13:39)</a>:</h4>
<p>although since MMIO is usually done through hardcoded addresses, it could be a bit weird to have a pointer to an atomic type at that location, no?</p>



<a name="171776212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171776212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171776212">(Jul 26 2019 at 14:05)</a>:</h4>
<p>FWIW I never understood why we need <code>AtomicXY</code> types, instead of having <code>ptr.atomic_load(...)</code> methods.</p>



<a name="171776295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171776295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171776295">(Jul 26 2019 at 14:06)</a>:</h4>
<p>Sure an atomic type wrapper can be built on top of the pointer methods, but the opposite is not true I think. </p>
<p>If you want to do an atomic relaxed load of some memory address in rust today, AFAICT, there is no easy way for you to do that</p>



<a name="171776318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171776318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171776318">(Jul 26 2019 at 14:06)</a>:</h4>
<p>You would need to somehow construct an <code>AtomicXY</code> at that address</p>



<a name="171776355"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171776355" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171776355">(Jul 26 2019 at 14:07)</a>:</h4>
<p>Well not construct, but go from the address as an <code>usize</code> to a <code>&amp;AtomicXY</code> that you can then operate on - doable, but weird</p>



<a name="171778185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171778185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171778185">(Jul 26 2019 at 14:28)</a>:</h4>
<p><code>*mut AtomicT</code> is the oposite you’re looking for.</p>



<a name="171778229"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171778229" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171778229">(Jul 26 2019 at 14:28)</a>:</h4>
<p>AtomicT is guaranteed to be layout-compatible to T so you can just cast the pointer.</p>



<a name="171779848"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171779848" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171779848">(Jul 26 2019 at 14:48)</a>:</h4>
<p>it’s just a bit trickier than having a method on the pointer itself. also i think you can still swap / replace the AtomicT non-atomically, which is a bit odd</p>



<a name="171780056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171780056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171780056">(Jul 26 2019 at 14:50)</a>:</h4>
<p>also you will have to go around casting your types to integers (or bool) in order to write them out with this design</p>



<a name="171780187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171780187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171780187">(Jul 26 2019 at 14:52)</a>:</h4>
<p>You can write an extension trait presumably that casts under the hood to the appropriate atomic and calls the appropriate function</p>



<a name="171780397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171780397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171780397">(Jul 26 2019 at 14:54)</a>:</h4>
<p>oh and there might be another issue - the methods for these would probably require taking &amp;self, but to my knowledge references must be dereferenceable at all times, and if you’re accessing some memory region that’s writable but not readable then it would be invalid to create a reference to this address</p>



<a name="171780467"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171780467" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171780467">(Jul 26 2019 at 14:55)</a>:</h4>
<p>i can’t think of any instance where this is actually the case in practice (writable but not readable mem), but there probably are cases</p>



<a name="171780782"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171780782" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171780782">(Jul 26 2019 at 14:59)</a>:</h4>
<p>I'm not sure why/how that would be a problem? I'm envisioning the methods on the trait taking <code>self</code> by-value so there's no references to speak of here</p>



<a name="171780797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171780797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171780797">(Jul 26 2019 at 14:59)</a>:</h4>
<p>We might want <code>AtomicT</code> and such to be defined on <code>*const T</code> instead of <code>&amp;self</code>, though</p>



<a name="171780803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171780803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171780803">(Jul 26 2019 at 14:59)</a>:</h4>
<p>and/or <code>*mut T</code></p>



<a name="171780879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171780879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171780879">(Jul 26 2019 at 15:00)</a>:</h4>
<p>I think there might be an open issue about that</p>



<a name="171780891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171780891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171780891">(Jul 26 2019 at 15:00)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> sorry, i was talking about <code>fn volatile_load(&amp;self, ...)</code> method on the AtomicT itself</p>



<a name="171780919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171780919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171780919">(Jul 26 2019 at 15:00)</a>:</h4>
<p>what gnzlbg proposed</p>



<a name="171780944"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171780944" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171780944">(Jul 26 2019 at 15:01)</a>:</h4>
<p>Yeah, we'd probably want that to be <code>fn volatile_load(*const Self, ...)</code></p>



<a name="171780956"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171780956" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171780956">(Jul 26 2019 at 15:01)</a>:</h4>
<p>then there should be no problem</p>



<a name="171780972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171780972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171780972">(Jul 26 2019 at 15:01)</a>:</h4>
<p>(though I guess we'd need that to be <code>unsafe fn</code>)</p>



<a name="171780975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171780975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171780975">(Jul 26 2019 at 15:01)</a>:</h4>
<p>ok cool</p>



<a name="171781018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171781018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171781018">(Jul 26 2019 at 15:02)</a>:</h4>
<p>does sound a bit tricky overall but i can’t think of a better way and it is niche anyway</p>



<a name="171781155"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171781155" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171781155">(Jul 26 2019 at 15:03)</a>:</h4>
<p>i guess this would need an RFC? i’m down to draft something up, but OTOH if people want to make other changes to ptr::volatile_load and such as ralf implied, then it may be better to do it all at once</p>



<a name="171781418"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171781418" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171781418">(Jul 26 2019 at 15:06)</a>:</h4>
<p>hm, not sure. I would lean towards yes since it deals with UCG stuff</p>



<a name="171784622"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171784622" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tom Phinney <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171784622">(Jul 26 2019 at 15:48)</a>:</h4>
<blockquote>
<p>i can’t think of any instance where this is actually the case in practice (writable but not readable mem), but there probably are cases</p>
</blockquote>
<p>Some embedded microcontrollers have memory-mapped I/O where a read-after-write returns a different value for the read than what was just written (e.g., writing bits that trigger actions, returning instead bits that reflect current state).</p>



<a name="171785645"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171785645" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171785645">(Jul 26 2019 at 16:02)</a>:</h4>
<p>i am thinking more of cases where an unnecessary read triggers an actual action, like crashing. i’m told that for the pci ide controller a read to some address indicates that you’ve handled an IRQ (presumably allowing it to send another one), which can be bad</p>



<a name="171785808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171785808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171785808">(Jul 26 2019 at 16:04)</a>:</h4>
<p>and again, i see absolutely no reason why this would even happen in practice, but if we’re being pedantic about non-tearing accesses it seems reasonable to be pedantic about this too</p>



<a name="171787099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171787099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tom Phinney <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171787099">(Jul 26 2019 at 16:24)</a>:</h4>
<blockquote>
<p>for the pci ide controller a read to some address indicates that you’ve handled an IRQ (presumably allowing it to send another one)</p>
</blockquote>
<p>I've encountered that situation in memory-mapped I/O. It tends to show up more frequently in 8/16-bit data, 16/24-bit <code>usize</code> microcontrollers, which Rust currently doesn't support (because the current minimum <code>usize</code> is 32). IMO this "feature" is often a hold-over from those earlier designs for small-bit-width word sizes. However, in cases like IRQ it may also be a timing optimization for performance-critical code that runs in a protected state (such as with interrupts disabled) whose duration needs to be minimized.</p>



<a name="171787426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171787426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171787426">(Jul 26 2019 at 16:29)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> </p>
<blockquote>
<p>atomic load/stores are non-tearing (they are "atomic"), and you just need to also prevent llvm from optimizing them in certain ways, which you can do by making them volatile</p>
</blockquote>
<p><code>atomic</code> has two aspects, non-tearing and the <code>Ordering</code> thing. So I don't think you want "atomic volatile", you just want "non-tearing volatile".</p>



<a name="171787565"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171787565" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171787565">(Jul 26 2019 at 16:31)</a>:</h4>
<p>what is the difference between a Relaxed volatile atomic access and a non-tearing volatile access? <span class="user-mention" data-user-id="120791">@RalfJ</span></p>



<a name="171787598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171787598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171787598">(Jul 26 2019 at 16:31)</a>:</h4>
<p><span class="user-mention" data-user-id="228354">@Matt Taylor</span> also for your questions around volatile and MMIO, some further reading material:<br>
- <a href="https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/49?u=ralfjung" target="_blank" title="https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/49?u=ralfjung">https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/49?u=ralfjung</a><br>
- <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/33" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/33">https://github.com/rust-lang/unsafe-code-guidelines/issues/33</a><br>
- <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/152" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/152">https://github.com/rust-lang/unsafe-code-guidelines/issues/152</a></p>



<a name="171787655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171787655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171787655">(Jul 26 2019 at 16:32)</a>:</h4>
<blockquote>
<p>what is the difference between a Relaxed volatile atomic access and a non-tearing volatile access? <span class="user-mention silent" data-user-id="120791">RalfJ</span></p>
</blockquote>
<p>the difference is in whether it is UB for such an access to be in a data race with other accesses</p>



<a name="171787669"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171787669" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171787669">(Jul 26 2019 at 16:32)</a>:</h4>
<p>and there's also a difference in whether a happens-before relationship can be established when this access is combined the right way with a fence</p>



<a name="171787700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171787700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171787700">(Jul 26 2019 at 16:33)</a>:</h4>
<p>I had a long discussion with <span class="user-mention" data-user-id="132920">@gnzlbg</span> about that once, but I am not sure where... we should find some place to write down the conclusions of that discussion</p>



<a name="171787785"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171787785" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171787785">(Jul 26 2019 at 16:34)</a>:</h4>
<blockquote>
<p>atomic has two aspects, non-tearing and the Ordering thing. So I don't think you want "atomic volatile", you just want "non-tearing volatile".</p>
</blockquote>
<p>Wasn't one of the ordering for atomics in LLVM <code>Unordered</code> ? (I'm not sure how that differed from <code>Relaxed</code> since <code>Relaxed</code> is defined as "no ordering")</p>



<a name="171787793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171787793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171787793">(Jul 26 2019 at 16:34)</a>:</h4>
<p><span class="user-mention" data-user-id="228354">@Matt Taylor</span> I am not sure how familiar you are with the C/C++ concurrency memory model(s)?</p>



<a name="171787840"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171787840" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171787840">(Jul 26 2019 at 16:35)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> yeah LLVM has an "even weaker than <code>Relaxed</code>" thing. IIRC it does not even guarantee coherence (i.e. it can first see a new write and then an old write). no idea how that relates to volatile or "normal" accesses though.</p>



<a name="171787931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171787931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171787931">(Jul 26 2019 at 16:36)</a>:</h4>
<p><span class="user-mention" data-user-id="228354">@Matt Taylor</span> ah here is the public part of that discussion: <a href="https://internals.rust-lang.org/t/add-volatile-operations-to-core-x86-64/10480" target="_blank" title="https://internals.rust-lang.org/t/add-volatile-operations-to-core-x86-64/10480">https://internals.rust-lang.org/t/add-volatile-operations-to-core-x86-64/10480</a></p>



<a name="171787935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171787935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171787935">(Jul 26 2019 at 16:36)</a>:</h4>
<p>but there's also a long private chat thread between <span class="user-mention" data-user-id="132920">@gnzlbg</span> and me</p>



<a name="171788098"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788098" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788098">(Jul 26 2019 at 16:39)</a>:</h4>
<p>I don't remember what the value was in doing something less than relaxed</p>



<a name="171788110"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788110" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788110">(Jul 26 2019 at 16:39)</a>:</h4>
<p>relaxed-&gt;fence-&gt;fence-&gt;relaxed can imply happens-before</p>



<a name="171788115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788115">(Jul 26 2019 at 16:39)</a>:</h4>
<p>Sure, using relaxed allows for some synchronization if the proper fences and other atomic operations are used</p>



<a name="171788121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788121">(Jul 26 2019 at 16:39)</a>:</h4>
<p>which I dont think we want for volatile</p>



<a name="171788191"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788191" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788191">(Jul 26 2019 at 16:40)</a>:</h4>
<p>i mean, it can, if one uses the fences</p>



<a name="171788208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788208">(Jul 26 2019 at 16:40)</a>:</h4>
<p>but then one wanted synchronization of some sort I guess, otherwise why use the fences</p>



<a name="171788219"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788219" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788219">(Jul 26 2019 at 16:40)</a>:</h4>
<p>yes. but the key thing is if you replace <code>Relaxed</code> by a "normal" access, it does not imply anything, even with the fences</p>



<a name="171788246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788246">(Jul 26 2019 at 16:41)</a>:</h4>
<p>yes that's right, a volatile atomic relaxed is not the same as a normal volatile load / store because of the fence interaction</p>



<a name="171788261"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788261" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788261">(Jul 26 2019 at 16:41)</a>:</h4>
<p>so without a very careful study I'd argue it should also not imply anything for volatile accesses</p>



<a name="171788274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788274">(Jul 26 2019 at 16:41)</a>:</h4>
<p>maybe <code>Unordered</code> is enough for that, not sure</p>



<a name="171788350"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788350" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788350">(Jul 26 2019 at 16:42)</a>:</h4>
<p><a href="https://llvm.org/docs/Atomics.html#unordered" target="_blank" title="https://llvm.org/docs/Atomics.html#unordered">https://llvm.org/docs/Atomics.html#unordered</a></p>



<a name="171788493"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788493" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788493">(Jul 26 2019 at 16:44)</a>:</h4>
<p>"These operations are required to be atomic in the sense that if you use unordered loads and unordered stores, a load cannot see a value which was never stored. "</p>



<a name="171788510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788510">(Jul 26 2019 at 16:44)</a>:</h4>
<p>"This cannot be used for synchronization"</p>



<a name="171788514"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788514" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788514">(Jul 26 2019 at 16:44)</a>:</h4>
<p>seems reasonable</p>



<a name="171788523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788523">(Jul 26 2019 at 16:44)</a>:</h4>
<p>"can be expensive or unavailable for wider loads"</p>



<a name="171788555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788555">(Jul 26 2019 at 16:45)</a>:</h4>
<p>It appears that it fails to compile if there isn't a native instruction that performs the operation atomically.</p>



<a name="171788562"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788562" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788562">(Jul 26 2019 at 16:45)</a>:</h4>
<p>probably volatile already acts like unordered, except for the tearing aspect</p>



<a name="171788579"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788579" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788579">(Jul 26 2019 at 16:45)</a>:</h4>
<p>"an unordered load or store cannot be split into multiple instructions "</p>



<a name="171788587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788587">(Jul 26 2019 at 16:45)</a>:</h4>
<p>but based on the feedback we got so far, it seems unlikely LLVM will want to guarantee that to us :/</p>



<a name="171788619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788619">(Jul 26 2019 at 16:45)</a>:</h4>
<p>FWIW on IRC they did recommend looking into atomic unordered instead if we wanted guaranteed no tearing</p>



<a name="171788680"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788680" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788680">(Jul 26 2019 at 16:46)</a>:</h4>
<p>we want no tearing <em>and</em> volatile though</p>



<a name="171788700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788700">(Jul 26 2019 at 16:46)</a>:</h4>
<p>but then these operations can be marked with <code>volatile</code></p>



<a name="171788708"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788708" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788708">(Jul 26 2019 at 16:46)</a>:</h4>
<p>unordered can be dead-read-eliminated, for example</p>



<a name="171788731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788731">(Jul 26 2019 at 16:46)</a>:</h4>
<p>LLVM lets you combine atomic orderings with <code>volatile</code>?</p>



<a name="171788732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788732">(Jul 26 2019 at 16:46)</a>:</h4>
<p>yes it would be volatile+unordered</p>



<a name="171788747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788747">(Jul 26 2019 at 16:46)</a>:</h4>
<p>yes it does, for all atomic orderings!</p>



<a name="171788754"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788754" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788754">(Jul 26 2019 at 16:46)</a>:</h4>
<p>oh wow</p>



<a name="171788763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788763">(Jul 26 2019 at 16:47)</a>:</h4>
<p>whatever the heck that means in terms of semantics^^</p>



<a name="171788767"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788767" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788767">(Jul 26 2019 at 16:47)</a>:</h4>
<p>so that's what I meant that volatile+relaxed might have done the trick</p>



<a name="171788786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788786">(Jul 26 2019 at 16:47)</a>:</h4>
<p>even though it might be a bit footguny if it can imply a happens before</p>



<a name="171788789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788789">(Jul 26 2019 at 16:47)</a>:</h4>
<p>I think if you <code>s/Relaxed/Unordered/</code>, I can live with that</p>



<a name="171788882"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788882" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788882">(Jul 26 2019 at 16:48)</a>:</h4>
<p>we could add unstable <code>volatile_unordered_load</code>/<code>store</code> methods to the Atomic types and see if those solve the problems that people have</p>



<a name="171788901"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788901" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788901">(Jul 26 2019 at 16:48)</a>:</h4>
<p>most are already doing what works for them, whatever that might be</p>



<a name="171788922"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171788922" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171788922">(Jul 26 2019 at 16:49)</a>:</h4>
<p>wow, this is a much bigger mess than i originally thought ^_^ thanks for the links, reading now</p>



<a name="171789037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789037">(Jul 26 2019 at 16:50)</a>:</h4>
<p>we could also add generic <code>volatile_load</code>/<code>store</code> methods to the atomic types that take an Ordering <span aria-label="laughter tears" class="emoji emoji-1f602" role="img" title="laughter tears">:laughter_tears:</span> <br>
I really have no idea why would one to use volatile with the other orderings</p>



<a name="171789397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789397">(Jul 26 2019 at 16:55)</a>:</h4>
<blockquote>
<p>wow, this is a much bigger mess than i originally thought ^_^ thanks for the links, reading now</p>
</blockquote>
<p>why is that the reaction I get almost every time I answer a question? <span aria-label="rofl" class="emoji emoji-1f923" role="img" title="rofl">:rofl:</span></p>



<a name="171789492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789492">(Jul 26 2019 at 16:56)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> or we could just make our existing intrinsics be <code>volatile unordered</code></p>



<a name="171789514"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789514" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789514">(Jul 26 2019 at 16:56)</a>:</h4>
<p>then we can entirely side-step the question of "what are the semantics of non-atomic volatile accesses" :D</p>



<a name="171789517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789517">(Jul 26 2019 at 16:56)</a>:</h4>
<p>I think that wouldn't work, it would fail to compile if the load can tear</p>



<a name="171789531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789531">(Jul 26 2019 at 16:56)</a>:</h4>
<p>oh dang</p>



<a name="171789566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789566">(Jul 26 2019 at 16:57)</a>:</h4>
<p>I'd like a version of <code>Unordered</code> that permits tearing but still does not have data race problems...</p>



<a name="171789606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789606">(Jul 26 2019 at 16:57)</a>:</h4>
<p>I think the tearing loads are not atomic</p>



<a name="171789665"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789665" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789665">(Jul 26 2019 at 16:58)</a>:</h4>
<p>you can probably implement them on top of the atomic unordered ones</p>



<a name="171789672"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789672" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789672">(Jul 26 2019 at 16:58)</a>:</h4>
<p>the ordering annotations make sense even with tearing</p>



<a name="171789719"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789719" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789719">(Jul 26 2019 at 16:58)</a>:</h4>
<p>they would say that all the individual "tears" would have that ordering</p>



<a name="171789735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789735">(Jul 26 2019 at 16:58)</a>:</h4>
<p>the ordering annotations explicitly say that one cannot observe values that haven't been written to I think</p>



<a name="171789751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789751">(Jul 26 2019 at 16:58)</a>:</h4>
<p>I don't think there is an ordering that doesn't say that</p>



<a name="171789765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789765">(Jul 26 2019 at 16:59)</a>:</h4>
<p>this would still apply "per-fragment" for the teared ones</p>



<a name="171789782"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789782" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789782">(Jul 26 2019 at 16:59)</a>:</h4>
<p>in fact we could just say it applies per byte</p>



<a name="171789793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789793">(Jul 26 2019 at 16:59)</a>:</h4>
<p>sure, but I don't know what would LLVM win from exposing them as intrinsics</p>



<a name="171789794"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789794" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789794">(Jul 26 2019 at 16:59)</a>:</h4>
<p>coalescing adjacent atomic accesses is sound, AFAIK</p>



<a name="171789876"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789876" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789876">(Jul 26 2019 at 17:00)</a>:</h4>
<p>basically I want to not ever have to think about non-atomic volatile accesses again</p>



<a name="171789895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789895">(Jul 26 2019 at 17:00)</a>:</h4>
<p>the question "how do volatile and atomic interact" comes up all the time</p>



<a name="171789907"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789907" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789907">(Jul 26 2019 at 17:00)</a>:</h4>
<p>if the answer is "like unordered but bytewise", that would be a big step IMO</p>



<a name="171789919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789919">(Jul 26 2019 at 17:00)</a>:</h4>
<p>I think we should just deprecate the volatile reads/writes  if the volatile atomic unordered intrinsics solve the problem</p>



<a name="171789931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789931">(Jul 26 2019 at 17:00)</a>:</h4>
<p>people that want tearing should just call those in a loop or something</p>



<a name="171789964"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789964" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789964">(Jul 26 2019 at 17:01)</a>:</h4>
<p>I don't know what problem tearing volatile load / stores solve</p>



<a name="171789971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171789971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171789971">(Jul 26 2019 at 17:01)</a>:</h4>
<p>(a) deprecation does not absolve us from defining their semantics, (b) we probably should still provide arbitrarily-sized volatile accesses "because C does"</p>



<a name="171790002"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171790002" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171790002">(Jul 26 2019 at 17:01)</a>:</h4>
<p>we can just wrap them to loop over the memory doing 1-byte wide volatile atomic unordered operations</p>



<a name="171790073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171790073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171790073">(Jul 26 2019 at 17:02)</a>:</h4>
<p>maybe something cleverer than that, like using the largest possible size or something</p>



<a name="171790084"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171790084" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171790084">(Jul 26 2019 at 17:02)</a>:</h4>
<p>very inefficient. but probably good enough for something deprecated.</p>



<a name="171790125"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171790125" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171790125">(Jul 26 2019 at 17:03)</a>:</h4>
<p>i mean, huge volatile load / stores generate horrible code already today</p>



<a name="171790145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171790145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171790145">(Jul 26 2019 at 17:03)</a>:</h4>
<p>yeah</p>



<a name="171790151"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171790151" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171790151">(Jul 26 2019 at 17:03)</a>:</h4>
<p>sounds like a solid plan to me</p>



<a name="171790153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171790153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171790153">(Jul 26 2019 at 17:03)</a>:</h4>
<p>i think there was an example of using one on an array of 4096 bytes and getting horrible assembly</p>



<a name="171790249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171790249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171790249">(Jul 26 2019 at 17:04)</a>:</h4>
<p>you would probably have to avoid doing it in 1 byte chunks not for performance but also just the fact that many people seem to erroneously rely on them for non-tearing for small types anyway</p>



<a name="171790283"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171790283" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171790283">(Jul 26 2019 at 17:05)</a>:</h4>
<ul>
<li>find a way to provide one uniform API for "non-tearing" accesses</li>
<li>use that to provide atomic and volatile accesses (the volatile ones being unordered implicitly)</li>
<li>remove the old intrinsics and implement everything in terms of the new shiny ones</li>
</ul>
<p>the user-visible API for non-tearing volatile accesses is still open I think?</p>



<a name="171790295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171790295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171790295">(Jul 26 2019 at 17:05)</a>:</h4>
<p>also <span class="user-mention" data-user-id="132920">@gnzlbg</span> <a href="https://github.com/rust-lang/rfcs/pull/2728" target="_blank" title="https://github.com/rust-lang/rfcs/pull/2728">https://github.com/rust-lang/rfcs/pull/2728</a> has been proposed, so the longer we wait the more messy volatile stuff already exists...</p>



<a name="171790373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171790373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171790373">(Jul 26 2019 at 17:06)</a>:</h4>
<blockquote>
<p>you would probably have to avoid doing it in 1 byte chunks not for performance but also just the fact that many people seem to erroneously rely on them for non-tearing for small types anyway</p>
</blockquote>
<p>shouldn't be too hard to do something like</p>
<div class="codehilite"><pre><span></span><span class="k">match</span><span class="w"> </span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="mi">1</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">...</span><span class="w"></span>
<span class="w">  </span><span class="mi">2</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">...</span><span class="w"></span>
<span class="w">  </span><span class="mi">4</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">...</span><span class="w"></span>
<span class="w">  </span><span class="mi">8</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">...</span><span class="w"></span>
<span class="w">  </span><span class="n">n</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="c1">// fallback, loop with largest power of 2 that is a factor of n</span>
<span class="w">  </span><span class="p">...</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="171790482"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171790482" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171790482">(Jul 26 2019 at 17:08)</a>:</h4>
<p>in fact if we "loop with largest power of 2 &lt;= size_of::&lt;usize&gt;() that is a factor of n", we'd get the expected behavior for sizes 2, 4, and on 64bit also size 8</p>



<a name="171791850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171791850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171791850">(Jul 26 2019 at 17:26)</a>:</h4>
<p>This would obviously be RFC material. I don't know who should give this idea some thought before we do that</p>



<a name="171793040"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171793040" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171793040">(Jul 26 2019 at 17:42)</a>:</h4>
<p>even if it’s only exposed via arch-dependent intrinsics, i believe it’s still worth considering volatile ops other than just load and store. for example on x86 i believe you need to use an OR when updating certain bits of page tables, and this strictly speaking should be done through volatile</p>



<a name="171793057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171793057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171793057">(Jul 26 2019 at 17:42)</a>:</h4>
<p>i’m not even sure if there are ways of doing this in rust currently</p>



<a name="171798089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171798089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171798089">(Jul 26 2019 at 18:25)</a>:</h4>
<p><span class="user-mention" data-user-id="228354">@Matt Taylor</span> but what is the problem with doing read, OR, store?</p>



<a name="171798119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171798119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171798119">(Jul 26 2019 at 18:25)</a>:</h4>
<p>is it because the CPU itself might concurrently modify the page table when the program does something?</p>



<a name="171798122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171798122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171798122">(Jul 26 2019 at 18:25)</a>:</h4>
<p>in this case it’s that the cpu can set a flag after you read</p>



<a name="171798127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171798127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171798127">(Jul 26 2019 at 18:25)</a>:</h4>
<p>yeah</p>



<a name="171798130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171798130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171798130">(Jul 26 2019 at 18:25)</a>:</h4>
<p>ah makes sense</p>



<a name="171798193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171798193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171798193">(Jul 26 2019 at 18:26)</a>:</h4>
<p>what do people do in C? I dont think it has volatile RMW operations</p>



<a name="171798208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171798208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171798208">(Jul 26 2019 at 18:26)</a>:</h4>
<p>(RMW = read-modify-write, i.e., all these atomic_and, atomic_add, atomic_or, compare_exchange, ...)</p>



<a name="171798241"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171798241" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171798241">(Jul 26 2019 at 18:26)</a>:</h4>
<p>it’s probably broken in C too lol</p>



<a name="171798245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171798245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171798245">(Jul 26 2019 at 18:27)</a>:</h4>
<p>that’s a good question though</p>



<a name="171798365"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171798365" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171798365">(Jul 26 2019 at 18:28)</a>:</h4>
<p>i mean <code>*some_volatile_var |= foo</code> is probably as close as it gets, but i guess at some point you just go for inline assembly</p>



<a name="171798425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171798425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171798425">(Jul 26 2019 at 18:28)</a>:</h4>
<p>i’ll try and find out what people are doing for this instance</p>



<a name="171798799"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171798799" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171798799">(Jul 26 2019 at 18:32)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> maybe instead of duplicating the entire API for volatile (with RMWs etc), we should have <code>Ordering::Volatile</code>?</p>



<a name="171799049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171799049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171799049">(Jul 26 2019 at 18:36)</a>:</h4>
<p>the problem is that the current API goes through &amp;self</p>



<a name="171799065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171799065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171799065">(Jul 26 2019 at 18:36)</a>:</h4>
<p>and since references are dereferenceable, it’s not going to work for MMIO i think?</p>



<a name="171799597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171799597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171799597">(Jul 26 2019 at 18:43)</a>:</h4>
<p>ah</p>



<a name="171799598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171799598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171799598">(Jul 26 2019 at 18:43)</a>:</h4>
<p>but there are problems for that even with atomic usage</p>



<a name="171799611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171799611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171799611">(Jul 26 2019 at 18:43)</a>:</h4>
<p>see <a href="https://github.com/rust-lang/rust/issues/55005" target="_blank" title="https://github.com/rust-lang/rust/issues/55005">https://github.com/rust-lang/rust/issues/55005</a></p>



<a name="171799629"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171799629" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171799629">(Jul 26 2019 at 18:43)</a>:</h4>
<p>(you didnt think we were already done with the messy parts, did you?^^)</p>



<a name="171799763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171799763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171799763">(Jul 26 2019 at 18:45)</a>:</h4>
<p>oh dear</p>



<a name="171799781"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171799781" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171799781">(Jul 26 2019 at 18:45)</a>:</h4>
<p>i think i need to have a lie down</p>



<a name="171809110"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171809110" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171809110">(Jul 26 2019 at 20:53)</a>:</h4>
<p>so <span class="user-mention" data-user-id="228354">@Matt Taylor</span> one proposal here is to make <code>UnsafeCell</code> not dereferencable. That would help with some things. An interesting open question is what to do about cases like <code>&amp;(i32, Cell&lt;i32&gt;)</code>; seems like at best we could mark the first 4 bytes dereferencable then. Though it would be nice if e.g. <code>Cell</code> could opt-back-in to <code>dereferencable</code>.<br>
But also see <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/88" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/88">https://github.com/rust-lang/unsafe-code-guidelines/issues/88</a>.</p>



<a name="171810426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171810426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tom Phinney <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171810426">(Jul 26 2019 at 21:12)</a>:</h4>
<p>TANSTAAFL, or you didn't think you'd solve the problem that easily, did you? <span aria-label="rofl" class="emoji emoji-1f923" role="img" title="rofl">:rofl:</span></p>



<a name="171811431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171811431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171811431">(Jul 26 2019 at 21:28)</a>:</h4>
<p>well I am just at the beginning of my research career, what would I do all day if there wouldn't be a large pool and a steady supply of open problems? ;)</p>



<a name="171815994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171815994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tom Phinney <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171815994">(Jul 26 2019 at 22:30)</a>:</h4>
<p>What would you do? I'm reminded of an old joke about a mathematician. I couldn't find that precise one online, but <a href="https://www.reddit.com/r/Jokes/comments/1kid90/the_mathematicians_interview/" target="_blank" title="https://www.reddit.com/r/Jokes/comments/1kid90/the_mathematicians_interview/">https://www.reddit.com/r/Jokes/comments/1kid90/the_mathematicians_interview/</a> comes close. <span aria-label="grinning" class="emoji emoji-1f600" role="img" title="grinning">:grinning:</span></p>



<a name="171817499"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171817499" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171817499">(Jul 26 2019 at 23:00)</a>:</h4>
<p>so I guess it's good for y'all that I don't have to create <em>new</em> problems to solve then :D</p>



<a name="171818446"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171818446" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171818446">(Jul 26 2019 at 23:20)</a>:</h4>
<blockquote>
<p>This would help with some things</p>
</blockquote>
<p>what things does it not solve here? seems like with a non-dereferenceable UnsafeCell and Ordering::Volatile that uses LLVM Unordered underneath, we’re set (in terms of soundness)</p>



<a name="171818620"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171818620" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171818620">(Jul 26 2019 at 23:24)</a>:</h4>
<p>what is it that i’ve neglected this time :p</p>



<a name="171833878"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171833878" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171833878">(Jul 27 2019 at 07:23)</a>:</h4>
<p><span class="user-mention" data-user-id="228354">@Matt Taylor</span> I think for MMIO that would be all, I was thinking of other problems around <code>dereferencable</code>, like the <code>Arc</code> one</p>



<a name="171833920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171833920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171833920">(Jul 27 2019 at 07:24)</a>:</h4>
<p>right</p>



<a name="171833935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171833935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171833935">(Jul 27 2019 at 07:25)</a>:</h4>
<p>another potential problem with this route is that it blocks people from using different Orderings than the one we choose for Volatile (Unordered)</p>



<a name="171834328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171834328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171834328">(Jul 27 2019 at 07:37)</a>:</h4>
<p>see <span class="user-mention" data-user-id="132920">@gnzlbg</span>'s question in the thread: what is the use-case for an access that is <em>both</em> synchronizing and volatile?</p>



<a name="171841923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171841923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171841923">(Jul 27 2019 at 11:36)</a>:</h4>
<p>(deleted)</p>



<a name="171841939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171841939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171841939">(Jul 27 2019 at 11:37)</a>:</h4>
<p>(deleted)</p>



<a name="171841986"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171841986" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171841986">(Jul 27 2019 at 11:38)</a>:</h4>
<p>(deleted)</p>



<a name="171841989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171841989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171841989">(Jul 27 2019 at 11:38)</a>:</h4>
<p>(deleted)</p>



<a name="171842000"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171842000" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171842000">(Jul 27 2019 at 11:39)</a>:</h4>
<p>volatile is orthogonal to atomics: it says that the reads and writes to memory must happen exactly has written in the code, e.g., if you read the same memory twice, those two reads must happen, if you read from memory once, only one read can happen, etc.</p>



<a name="171842007"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171842007" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171842007">(Jul 27 2019 at 11:39)</a>:</h4>
<p>the compiler must assume that the reads and writes have side-effects like unknown function calls, etc.</p>



<a name="171842056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171842056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171842056">(Jul 27 2019 at 11:40)</a>:</h4>
<p>That pretty much inhibits all optimizations, so why would want to on top of that combine the volatile reads / writes with atomics ?</p>



<a name="171842064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171842064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171842064">(Jul 27 2019 at 11:40)</a>:</h4>
<p>No idea. If you have volatile and atomic operations, the compiler might be able to re-order the atomic ones across the volatile ones in certain ways. By making the atomic ones volatile, you inhibit that.</p>



<a name="171842134"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171842134" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171842134">(Jul 27 2019 at 11:44)</a>:</h4>
<p>Somebody on LLVM IRC mentioned that "volatile synchronizes with volatile" because you can't reorder volatile operations across each other</p>



<a name="171880773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171880773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171880773">(Jul 28 2019 at 08:44)</a>:</h4>
<blockquote>
<p>volatile is orthogonal to atomics: it says that the reads and writes to memory must happen exactly has written in the code, e.g., if you read the same memory twice, those two reads must happen, if you read from memory once, only one read can happen, etc.</p>
</blockquote>
<p>agreed. slightly more formally speaking: it makes reads and write observable events, and thus part of the program behavior that must be preserved.</p>



<a name="171880777"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171880777" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171880777">(Jul 28 2019 at 08:44)</a>:</h4>
<blockquote>
<p>Somebody on LLVM IRC mentioned that "volatile synchronizes with volatile" because you can't reorder volatile operations across each other</p>
</blockquote>
<p>I think that's very wrong. "synchronizes with" also means that <em>other</em> instructions cannot be reordered around this. that is explicitly <em>not</em> true for <code>volatile</code>.</p>



<a name="171880809"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171880809" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171880809">(Jul 28 2019 at 08:46)</a>:</h4>
<p>IOW, the following code is wrong:</p>
<div class="codehilite"><pre><span></span>static int DATA = 0;
volatile int FLAG = 0;
</pre></div>


<p>thread A:</p>
<div class="codehilite"><pre><span></span>DATA = 42;
FLAG = 1;
</pre></div>


<p>thread B:</p>
<div class="codehilite"><pre><span></span>while (FLAG == 0) {}
printf(&quot;%d&quot;, DATA);
</pre></div>



<a name="171880834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171880834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171880834">(Jul 28 2019 at 08:46)</a>:</h4>
<p>if volatile synced-with volatile, then that code would be fine, but in fact the compiler is allowed to reorder the two writes in thread A</p>



<a name="171881060"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171881060" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171881060">(Jul 28 2019 at 08:54)</a>:</h4>
<p>isn’t their point that you can’t reorder a volatile around <em>other volatiles</em>? in your case, there is only one volatile, but if you make data volatile then it would be forced to not reorder the writes</p>



<a name="171881284"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171881284" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171881284">(Jul 28 2019 at 09:01)</a>:</h4>
<p>yes, they are saying "X and X implies Y, hence Y" where X = "volatiles cannot be reodered with each other" and Y = "volatiles sync with each other"</p>



<a name="171881286"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171881286" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171881286">(Jul 28 2019 at 09:01)</a>:</h4>
<p>I agree with "X" but I do not agree with "X implies Y", and hence I do not agree with "Y"</p>



<a name="171881341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171881341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171881341">(Jul 28 2019 at 09:02)</a>:</h4>
<p>"sync withe each other" has a very precise meaning in concurrent memory models. it's what release/acquire accesses (amongst other things do)</p>



<a name="171881344"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171881344" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171881344">(Jul 28 2019 at 09:02)</a>:</h4>
<p>if you replace the volailte load/store in my example by release/acquire load store, it becomes correct</p>



<a name="171881349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171881349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171881349">(Jul 28 2019 at 09:03)</a>:</h4>
<p>yep, it's definitely wrong that volatile synchronizes-with volatile in that sense</p>



<a name="171881355"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171881355" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171881355">(Jul 28 2019 at 09:03)</a>:</h4>
<p>it becomes correct because you cannot move the store to DATA down below the release-store to FLAG</p>



<a name="171881357"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171881357" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171881357">(Jul 28 2019 at 09:03)</a>:</h4>
<p><em>that</em> is what it means for accesses to synchronize</p>



<a name="171881360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171881360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171881360">(Jul 28 2019 at 09:03)</a>:</h4>
<p>i think they were just using it too loosely. either way, i think we’re in agreement here</p>



<a name="171881403"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171881403" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171881403">(Jul 28 2019 at 09:04)</a>:</h4>
<p><span class="user-mention" data-user-id="228354">@Matt Taylor</span> I agree that we two are in agreement. they might "just" have said it to loosely but the business of specifying a language it not one where you can permit yourself to be loose about your terminology.</p>



<a name="171881405"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171881405" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171881405">(Jul 28 2019 at 09:04)</a>:</h4>
<p>yeah, absolutely</p>



<a name="171881421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171881421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171881421">(Jul 28 2019 at 09:05)</a>:</h4>
<p>so, I am not being nitpicky to annoy people, and I dont think they are being a bad person by being loose about terminology, I just try to make sure we all use the same terms to mean the same things, in particular terms that actually <em>do</em> have a precise meaning :)</p>



<a name="171881489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171881489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matt Taylor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171881489">(Jul 28 2019 at 09:07)</a>:</h4>
<p>I should probably read up a bit more on C’s concurrent memory model</p>



<a name="171881914"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171881914" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171881914">(Jul 28 2019 at 09:22)</a>:</h4>
<p>are you sure you want that? hint: if you thought <code>volatile</code> was messy...</p>



<a name="171881916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171881916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171881916">(Jul 28 2019 at 09:23)</a>:</h4>
<p>the good news is that the concurrency mess is different :P</p>



<a name="171881922"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile%2C%20atomics%20and%20mmio/near/171881922" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/volatile.2C.20atomics.20and.20mmio.html#171881922">(Jul 28 2019 at 09:23)</a>:</h4>
<p>the problem is not that we have no formal model, the problem is we have a dozen of them that all have different flaws</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>